package com.partner.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * ʵ��date��string�Լ�calendar֮��Ļ���ת��
 * @author Jinshuan
 *
 */
public class DateUtil {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm");
	private static SimpleDateFormat sdf4 = new SimpleDateFormat("MM-dd");
	private static SimpleDateFormat sdf5 = new SimpleDateFormat("yyyy/MM/dd");
	private static SimpleDateFormat sdf6 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	//��stringתΪ��������
	public  static Calendar stringToCalendar(String str){
		Date date = stringToDate(str);
		Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
		 return cal;
	}
	
	//�����ڱ��string����
		public static String dateToString(Date date){
			String str = sdf.format(date);
			return str;
		}
		//�����ڱ�Ϊstring���ͣ�������/�ָ�
		public static String dateToString2(Date date){
			String str = sdf5.format(date);
			return str;
		}
		public static String dateToString3(Date date){
			String str1 = sdf6.format(date);
			return str1;
		}
		//��stringתΪdate����
		public static Date stringToDate(String str){
			Date date = new Date();
			try {
				date = sdf.parse(str);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				date = new Date();
			}
			return date;
		}
		
		//���������͵�ֵ����ʱ����תΪ������ʽ
		public static Date StringToDate2(String str){
			Date date = new Date();
			try {
				date = sdf2.parse(str);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return date;
		}
		
		
		//��string����תΪY-M-D
		public static String StringToYMD(String str){
			String resut = dateToString(stringToDate(str));
			return resut;
		}
		public static String StringToYMD2(String str){
			String resut = dateToString2(stringToDate(str));
			return resut;
		}
		//��string����תΪʱ��
		public static String stringToHM(String str){
			Date date = StringToDate2(str);
			return sdf3.format(date);
		}
		//������תΪ���ڼ�
		public static String DateToXingqiji(Date date){
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			return numberToString(cal.get(Calendar.DAY_OF_WEEK));
		}
		
		//���ַ�תΪ���ڼ�
		public static String stringToXingqiji(String str){
			Calendar cal = Calendar.getInstance();
			cal.setTime(stringToDate(str));
			return numberToString(cal.get(Calendar.DAY_OF_WEEK));
		}
		//������תΪ����
		public static String StringToMD(String str){
			Date date = stringToDate(str);
			return sdf4.format(date);
		}
		
//		//����������������
//		public static int daysOfTwo(Date fDate, Date oDate) {
//
//		       Calendar aCalendar = Calendar.getInstance();
//
//		       aCalendar.setTime(fDate);
//
//		       int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
//		       aCalendar.setTime(oDate);
//		       int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
//
//		       return day2 - day1;
//
//		    }
		//��������������������
		public static int compareTwoDays(String day1,String day2){
			try {
				Date date1 = sdf.parse(day1);
				Date date2 = sdf.parse(day2);
				long date1_long = date1.getTime();
				long date2_long = date2.getTime();
				long times = date2_long-date1_long;
				int days = (int) (times/(1000*60*60*24));
				return days;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		}
		
		//��������ʱ�����ķ�����,���ص��Ǻ�����
				public static long compareTwoTime(String day1,String day2){
					try {
						Date date1 = sdf6.parse(day1);
						Date date2 = sdf6.parse(day2);
						long date1_long = date1.getTime();
						long date2_long = date2.getTime();
						long times = date2_long-date1_long;
						return times;
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
						
				return 0;
				}
		// ������תΪ���ڼ�
		public static String numberToString(int number) {
			if (1 == number) {
				return "����";
			} else if (2 == number) {
				return "��һ";
			} else if (3 == number) {
				return "�ܶ�";
			} else if (4 == number) {
				return "����";
			} else if (5 == number) {
				return "����";
			} else if (6 == number) {
				return "����";
			} else if (7 == number) {
				return "����";
			}
			return "";

		}
}
